﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using D3DSS.DataContract;
using D3DSS.Enums;
using D3DSS.Http;
using D3DSS.Entity;
using System.Configuration;
using D3DSS.FormUI.Properties;

namespace D3DSS.FormUI
{
    public partial class frmStatisticsOrder : Form
    {
        public frmStatisticsOrder()
        {
            InitializeComponent();
        }

        private void frmStatisticsOrder_Load(object sender, EventArgs e)
        {
            cmbAct.SelectedIndex = Settings.Default.LastStatisticsOrderAct;
            cmbGameRole.SelectedIndex = Settings.Default.LastStatisticsOrderGameRole;
            cmbGameServer.SelectedIndex = Settings.Default.LastStatisticsOrderGameServer;
            nudMF.Value = Settings.Default.LastStatisticsOrderMF;
            var roomIP = IPHelper.GetRoomIP();
            nudRoomIP.Value = roomIP.Item2;
            nudRoomParentIP.Value = roomIP.Item1;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            var request = new StatisticsOrderRequest();

            request.UserID = Program.Token.UserID;
            request.Act = (ActType)(cmbAct.SelectedIndex + 1);
            request.GameRole = (GameRoleType)(cmbGameRole.SelectedIndex + 1);
            request.GameServer = (GameServerType)(cmbGameServer.SelectedIndex + 1);
            request.MF = (int)nudMF.Value;
            request.RoomIP = (int)nudRoomIP.Value;
            request.RoomParentIP = (int)nudRoomParentIP.Value;

            var response = HttpRequest.Post<Response<StatisticsOrderResponse>>(Settings.Default.CreateStatisticsOrderUrl, request, Program.Token);
            if (response.Item1.IsSuccess)
            {
                Program.StatisticsOrder = new StatisticsOrder();
                Program.StatisticsOrder.StatisticsOrderID = response.Item1.Data.StatisticsOrderID;
                Program.StatisticsOrder.Act = cmbAct.Text;
                Program.StatisticsOrder.GameRole = cmbGameRole.Text;
                Program.StatisticsOrder.GameServer = cmbGameServer.Text;
                Program.StatisticsOrder.MF = ((int)nudMF.Value).ToString();
                Program.StatisticsOrder.RoomIP = ((int)nudRoomParentIP.Value).ToString() + "." + ((int)nudRoomIP.Value).ToString();

                Settings.Default.LastStatisticsOrderAct = cmbAct.SelectedIndex;
                Settings.Default.LastStatisticsOrderGameRole = cmbGameRole.SelectedIndex;
                Settings.Default.LastStatisticsOrderGameServer = cmbGameServer.SelectedIndex;
                Settings.Default.LastStatisticsOrderMF = (int)nudMF.Value;

                this.Close();
            }
            else
            {
                MessageBox.Show(response.Item1.MessageDictionary.Single(m => m.MessageCode == response.Item1.Code).MessageContent);
            }
        }


    }
}
